﻿namespace ExceptionExercise
{
    using System;
    using System.Globalization;

    public class MainExceptionExercise
    {
        //// Define a class InvalidRangeException<T> that holds information about an error condition related to invalid range. It should
        //// hold error message and a range definition [start … end]. Write a sample application that demonstrates the InvalidRangeException<int>
        //// and InvalidRangeException<DateTime> by entering numbers in the range [1..100] and dates in the range [1.1.1980 … 31.12.2013].

        private const string DateFormat = "dd.M.yyyy";

        private static void Main()
        {
            byte startNum = 1;
            byte endNum = 5;

            try
            {
                int inputNum = int.Parse(Console.ReadLine());

                if (inputNum < startNum || inputNum > endNum)
                {
                    throw new InvalidRangeException<int>("Out of range", startNum, endNum);
                }
            }
            catch (InvalidRangeException<int> exception)
            {
                Console.WriteLine("{0}{1}The number is out of the range [{2}...{3}]", exception.Message, Environment.NewLine, exception.StartRange, exception.EndRange);
            }
            finally
            {
                Console.WriteLine("Best regards 1/2");
            }

            DateTime startDate = new DateTime(1980, 1, 1);
            DateTime endDate = new DateTime(2013, 12, 31);

            try
            {
                string inputDate = Console.ReadLine();
                DateTime date = DateTime.ParseExact(inputDate, DateFormat, CultureInfo.InvariantCulture);

                if (date < startDate || date > endDate)
                {
                    throw new InvalidRangeException<DateTime>("Out of range", startDate, endDate);
                }
            }
            catch (InvalidRangeException<DateTime> exception)
            {
                Console.WriteLine("{0}{1}The date is out of the range: [{2} - {3}]", exception.Message, Environment.NewLine, exception.StartRange.ToString(DateFormat), exception.EndRange.ToString(DateFormat));
            }
            finally
            {
                Console.WriteLine("Best regards 2/2");
            }
        }
    }
}